package com.atguigu.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author : leimingchao
 * @date Date : 2021年10月16日 9:49
 */
@Controller
public class LoginController {

    @PostMapping(value = "/user/login")
    // @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public String login(@RequestParam(value = "username") String username,
                        @RequestParam(value = "password")String password,
                        Map<String,Object> map, HttpSession httpSession) {
        if (!StringUtils.isEmpty(username) && "123456".equals(password)) {
            //登录成功,防止表单重复提交，可以重定向到主页
            httpSession.setAttribute("loginUser",username);
            return "redirect:/main.html";
        } else {
            //登录失败
            map.put("msg","用户名或密码错误");
            return "login";
        }
    }
}
